1 00:00:00,430 --> 00:00:01,150 Hey there. 2 00:00:01,150 --> 00:00:02,890 Thanks for making it to this lecture. 3 00:00:02,890 --> 00:00:07,330 This lecture is going to be a little short as we're just going to talk about what polymorphism is, 4 00:00:07,330 --> 00:00:13,450 but we won't really practice any examples of polymorphism in Lua as there is no practical use of this 5 00:00:13,450 --> 00:00:14,500 concept in Lua. 6 00:00:14,500 --> 00:00:19,990 Nor is there any good example that I can give you in Lua, as its effects only exist because of inheritance, 7 00:00:19,990 --> 00:00:21,550 which we've already learned about. 8 00:00:21,550 --> 00:00:25,930 So I'm just going to give you a simple explanation of polymorphism, and this is an idea that you can 9 00:00:25,930 --> 00:00:30,490 carry along with you if you learn some object oriented language like C plus, plus or C sharp. 10 00:00:30,940 --> 00:00:36,580 Anyways, polymorphism is an object oriented programming concept that allows objects of different classes 11 00:00:36,580 --> 00:00:41,950 to be treated as if they were of the same class or in other words, have classes, inherit the same 12 00:00:41,950 --> 00:00:44,560 functions, yet act differently. 13 00:00:44,560 --> 00:00:46,060 How is this achieved? 14 00:00:46,060 --> 00:00:52,930 Well, the classes would use a common interface or a set of methods known as an abstract class containing 15 00:00:52,930 --> 00:00:54,310 abstract functions. 16 00:00:54,310 --> 00:00:56,380 An object oriented programming languages. 17 00:00:56,380 --> 00:01:00,250 There's a dedicated way of doing this, but we can't really do it in Lua. 18 00:01:00,250 --> 00:01:05,800 How it's typically done is by creating what we would call an abstract class, because it doesn't really 19 00:01:05,800 --> 00:01:10,780 stand for anything, and the only purpose of it is to contain abstract functions that can be commonly 20 00:01:10,780 --> 00:01:12,280 shared among other classes. 21 00:01:12,280 --> 00:01:17,170 In fact, an abstract class doesn't even contain any implementation for any functions. 22 00:01:17,170 --> 00:01:22,960 The only purpose of an abstract class is just to contain a blueprint or a contract for what a class 23 00:01:22,960 --> 00:01:28,240 should contain, and from that point, the classes that inherit those blueprinted functions have to 24 00:01:28,240 --> 00:01:30,520 create the functionality for it themselves. 25 00:01:30,850 --> 00:01:33,820 So here's an example of polymorphism that I got working. 26 00:01:33,820 --> 00:01:36,280 But at the end of the day it's actually just inheritance. 27 00:01:36,280 --> 00:01:42,580 Anyways, so I've created this quote unquote abstract class because we can't actually make any in Lua. 28 00:01:42,580 --> 00:01:49,270 But what this abstract class is doing is defining some functions that some shapes like a circle class 29 00:01:49,270 --> 00:01:53,470 or a rectangle class or whatever, should have if we were to make those classes. 30 00:01:53,470 --> 00:02:00,100 So these classes should have a constructor, and they should have a draw function to define the position 31 00:02:00,100 --> 00:02:05,950 of the shape inside of these there is no functionality, it just errors. 32 00:02:05,950 --> 00:02:13,780 Just in case, maybe the class that is inheriting these blueprinted or abstract functions hasn't implemented 33 00:02:13,780 --> 00:02:18,370 functionality for these functions yet, so it's just going to error and say that this function has not 34 00:02:18,370 --> 00:02:19,390 been implemented. 35 00:02:19,930 --> 00:02:23,260 And then what I've done here is I've created a circle class. 36 00:02:23,260 --> 00:02:27,130 Set the index meta method to itself so we can access these functions. 37 00:02:27,130 --> 00:02:30,970 And then I've set the meta table for the circle class to the shape class. 38 00:02:31,540 --> 00:02:37,330 That means if we were to ever index any functions inside of the circle class that did not exist, but 39 00:02:37,330 --> 00:02:43,210 they existed within the shape class, it'll look into the shape class, see that this function exists, 40 00:02:43,210 --> 00:02:49,180 and then it will error saying, hey, this class here hasn't implemented any functionality for, let's 41 00:02:49,180 --> 00:02:51,730 say, the constructor or this draw function. 42 00:02:52,570 --> 00:02:55,480 So inside of these functions you know, I've implemented some stuff. 43 00:02:55,480 --> 00:02:56,770 It doesn't just error. 44 00:02:56,920 --> 00:03:00,550 When I create a new circle we set the meta table to the circle class. 45 00:03:00,550 --> 00:03:03,100 We set the x, the y and the radius. 46 00:03:03,100 --> 00:03:04,540 And then we just return it. 47 00:03:05,220 --> 00:03:10,110 And then for the draw function, it just prints the location of the circle and it tells us the radius 48 00:03:10,110 --> 00:03:11,010 of the circle. 49 00:03:11,040 --> 00:03:17,550 So when I create this new circle here has a position on the x axis of three, on the y axis of five, 50 00:03:17,550 --> 00:03:19,650 and it has a radius of ten. 51 00:03:19,650 --> 00:03:23,460 And when I go to draw the function it should print that right on the console for us. 52 00:03:23,460 --> 00:03:24,690 So when I hit run. 53 00:03:25,550 --> 00:03:26,420 There we go. 54 00:03:26,450 --> 00:03:30,920 We have a circle at X of three, y of five, and a radius of ten. 55 00:03:31,790 --> 00:03:34,520 But let's say I didn't implement this draw function. 56 00:03:34,520 --> 00:03:40,040 What if I were to just comment out and this draw function didn't exist within the circle class? 57 00:03:40,070 --> 00:03:46,760 Well, what's going to happen is that the interpreter is going to check for this draw, um, index here. 58 00:03:46,760 --> 00:03:51,080 It's going to look inside of this object and see it has this meta table of circle. 59 00:03:51,080 --> 00:03:55,760 It's going to go in here and see if the circle class has the draw function which it doesn't because 60 00:03:55,760 --> 00:03:57,020 I commented it out. 61 00:03:57,020 --> 00:03:59,060 Then it'll see okay. 62 00:03:59,060 --> 00:04:04,970 Well this circle class does have another meta table which is shape which is inheritance by the way. 63 00:04:04,970 --> 00:04:08,900 And it'll look inside the shape for an index meta method which it has. 64 00:04:08,900 --> 00:04:12,830 So it'll check this table and see that the function exists in here. 65 00:04:13,280 --> 00:04:18,740 And because we didn't put any implementation within the circle class for the draw function, it's just 66 00:04:18,740 --> 00:04:22,430 going to error and tell us that this function has not been implemented. 67 00:04:22,760 --> 00:04:25,010 So if we go and run the game. 68 00:04:25,790 --> 00:04:26,630 There we go. 69 00:04:26,630 --> 00:04:30,680 We get an error and it says draw function has not yet been implemented. 70 00:04:32,000 --> 00:04:36,740 And we can also do the same thing for the constructor. 71 00:04:36,740 --> 00:04:42,560 If we were to comment out the constructor and run the game, it'll tell us a new function has not yet 72 00:04:42,560 --> 00:04:43,610 been implemented. 73 00:04:44,100 --> 00:04:50,820 So the only purpose of this shape class is to be an abstract class that's telling us what our shape 74 00:04:50,820 --> 00:04:57,540 classes like for circles, or for rectangles or whatever, that they should have these functions in 75 00:04:57,570 --> 00:04:58,350 them by default. 76 00:04:58,350 --> 00:05:02,610 And if they don't, it's going to error and tell them that this function has not been implemented. 77 00:05:02,610 --> 00:05:07,680 But realistically, there is no practical use of polymorphism in Lua. 78 00:05:07,710 --> 00:05:11,430 The only reason why this is working is because we're using inheritance. 79 00:05:12,060 --> 00:05:17,430 So in short, you don't need to worry about polymorphism, but it's going to be an important concept. 80 00:05:17,430 --> 00:05:21,060 You'll want to learn about it for other languages that you might want to learn in the future. 81 00:05:21,090 --> 00:05:23,910 Otherwise, that's all for this lecture and I'll see you in the next one.